window.onload = function() {
	getLatLng(function(position) {
		loadWeather(position.coords.latitude, position.coords.longitude);
		updateSubstance(position.coords.latitude, position.coords.longitude);
	}, function() {
		alert("Cannot get your location");
	});
};

function getLatLng(success, failure) {
	if ("geolocation" in navigator) {
		var geo_options = {
			enableHighAccuracy : true,
			maximumAge : 30000,
			timeout : 27000
		};
		navigator.geolocation.getCurrentPosition(success, failure, geo_options);
	} else {
		failure();
	}

}

function updateSubstance(lat, lng) {
	$.ajax({
		type : "GET",
		url : "data",
		dataType : "json",
		data : {
			current : true,
			latitude : lat,
			longitude : lng
		}
	}).done(function(json) {
		updatePollutionData(json);
	});
}

function loadWeather(lat, lng) {
	var url = 'http://api.wunderground.com/api/3b4c284cf1faa2dd/conditions/q/'
			+ lat + "," + lng + '.json';
	$.ajax({
		type : "GET",
		url : url,
		dataType : "jsonp"
	}).done(function(json) {
		updateWeather(json);
	});
}

function updateWeather(json) {
	var o = json.current_observation;
	$("#curIcon img").attr("src", o.icon_url);
	$("#curCond span").html(o.weather);
	$("#curTemp .wx-value").html(o.temp_f);
	$("#curFeel .wx-value").html(o.temp_f);
	$("#curFeel .wx-value").html(o.feelslike_f);
	$("#curWind").html(o.wind_string);
	$("#curHumidity").html("Humidity: " + o.relative_humidity);

}

function updatePollutionData(json) {
	var substance_list = [ "no2", "pm10", "so2", "o3", "pm2p5" ];
	for (var i = 0; i < substance_list.length; i++) {
		updateSubstanceInfo(substance_list[i], json);
	}
}

function updateSubstanceInfo(name, json) {
	var val = Math.round(json[name] * 100) / 100;// making it two decimal
	// places
	var className = getClass(val, name);
	var polLevel = getPollutionLevel(val, name);
	$("#" + name).find(".aq-indicator, .reading").addClass(className);
	$("#" + name + " .reading.badge").html(
			val + "<sub>&mu;g/m<sup>3</sup></sub>");
	$("#" + name + " .aq-indicator.badge").html(polLevel);
	// $("#" + name + " .aq-indicator.badge").html(className.substring(0,
	// className.indexOf("-")));
}

function getPollutionLevel(value, substance) {
	var boundaries = [];
	switch (substance) {
	case "o3":
		boundaries = [ 0, 34, 67, 101, 121, 141, 161, 188, 214, 241 ];
		break;
	case "no2":
		boundaries = [ 0, 68, 135, 201, 268, 335, 401, 468, 535, 601 ];
		break;
	case "so2":
		boundaries = [ 0, 89, 178, 267, 355, 444, 533, 711, 888, 1065 ];
		break;
	case "pm2p5":
		boundaries = [ 0, 12, 24, 36 - 41, 42, 48, 54, 59, 65, 71 ];
		break;
	case "pm10":
		boundaries = [ 0, 17, 34, 51, 59, 67, 76, 84, 92, 101 ];
		break;
	default:
		return -1;
	}
	var i = 0;
	for (; i < boundaries.length; i++) {
		if (value < boundaries[i]) {
			break;
		}
	}
	return i - 1;
}

function getClass(value, substance) {
	var classNames = [ "low-1", "low-2", "low-3", "moderate-1", "moderate-2",
			"moderate-3", "high-1", "high-2", "high-3", "very-high" ];
	var level = getPollutionLevel(value, substance);
	if (level == -1)
		return "na";
	return classNames[level];
}

function getTime(d) {
	var AM = true;
	var hours = d.getHours();
	if (hours >= 12) {
		AM = false;
		hours = hours - 12;
	}

	if (hours < 10) {
		hours = "0" + hours;
	}

	var minutes = d.getMinutes();
	if (minutes < 10) {
		minutes = "0" + minutes;
	}
	return hours + ":" + minutes + AM ? " AM" : " PM";
}